package com.ailatrieuphu.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.ailatrieuphu.CMD;
import com.ailatrieuphu.IAction;
import com.ailatrieuphu.Message;
import com.ailatrieuphu.MyActivity;
import com.ailatrieuphu.R;
import com.ailatrieuphu.adapter.RoomAdapter;
import com.ailatrieuphu.item.Room;
import com.ailatrieuphu.item.Table;

public class ActivityRoom extends MyActivity {

	EditText etRoom;
	Button btRoom, btRoomOut;
	Button btTableGetList;
	Button btTableJoin;
	Button btTableOut;
	int idRoom;
	ArrayList<Room> items;
	ListView listView;

	// Thieu out room

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_room);
		items = new ArrayList<Room>();
		listView = (ListView) findViewById(R.id.room_listRoom);
		C_PLAYER_GET_LIST_ROOM();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				idRoom = position;
				C_PLAYER_JOIN_ROOM(position);
			}
		});

		// etRoom = (EditText) findViewById(R.id.room_etRoom);
		// btRoom = (Button) findViewById(R.id.room_btRoom);
		// btRoomOut = (Button) findViewById(R.id.room_btRoomOut);
		// btTableGetList = (Button) findViewById(R.id.room_btTableGetList);
		// btTableJoin = (Button) findViewById(R.id.room_btTableJoin);
		// btTableOut = (Button) findViewById(R.id.room_btTableOut);
		//
		// btRoom.setOnClickListener(this);
		// btRoomOut.setOnClickListener(this);
		// btTableGetList.setOnClickListener(this);
		// btTableJoin.setOnClickListener(this);
		// btTableOut.setOnClickListener(this);

	}

	// @Override
	// public void onClick(View v) {
	// switch (v.getId()) {
	// case R.id.room_btRoom:
	// idRoom = Integer.parseInt(etRoom.getText().toString());
	// C_PLAYER_JOIN_ROOM(idRoom);
	// break;
	// case R.id.room_btRoomOut:
	// C_PLAYER_OUT_ROOM(idRoom);
	// break;
	// case R.id.room_btTableGetList:
	// C_PLAYER_GET_LIST_TABLE_INFO(idRoom);
	// break;
	// case R.id.room_btTableJoin:
	// int idTable = Integer.parseInt(etRoom.getText().toString());
	// C_PLAYER_JOIN_TABLE(idTable, idRoom);
	// break;
	// case R.id.room_btTableOut:
	// int idd = Integer.parseInt(etRoom.getText().toString());
	// C_PLAYER_BACK_ROOM(idd);
	// break;
	// default:
	// break;
	// }
	// }

	public void C_PLAYER_JOIN_TABLE(int idTable, int idRoom) {
		Message message = new Message(CMD.C_PLAYER_JOIN_TABLE);
		message.writeInt(idRoom);
		message.writeInt(idTable);
		connection.sendMessage(message);
	}

	public void C_PLAYER_BACK_ROOM(int idTable) {
		Message message = new Message(CMD.C_PLAYER_BACK_ROOM);
		connection.sendMessage(message);
	}

	public void S_PLAYER_JOIN_TABLE(Message message) {
		boolean joinTable = message.readBoolean();
		String alert = message.readString();
		Log.e("S_PLAYER_JOIN_TABLE: " + joinTable, alert);
	}

	public void C_PLAYER_GET_LIST_TABLE_INFO(int idRoom) {
		Message message = new Message(CMD.C_PLAYER_GET_LIST_TABLE_INFO);
		message.writeInt(idRoom);
		connection.sendMessage(message);
	}

	private Table[] tables;

	public void S_PLAYER_GET_LIST_TABLE_INFO(Message message) {
		int lengthTable = message.readInt();
		if (tables == null) {
			tables = new Table[lengthTable];
		}
		for (int i = 0; i < lengthTable; i++) {
			Table table = new Table();
			table.setIdTable(message.readInt());
			table.setBet(message.readInt());
			table.setTitle(message.readString());
			table.setCountPlayer(message.readInt());
			table.setMaxPlayer(message.readInt());
			tables[i] = table;
			Log.e("Table: " + i, table.toString());
		}
	}

	public void S_PLAYER_GET_LIST_ROOM(Message message) {
		int roomCount = message.readInt();
		for (int i = 0; i < roomCount; i++) {
			Room room = new Room();
			room.setName(message.readString());
			room.setStatus(message.readInt());
			room.setMinBet(message.readInt());
			items.add(room);
		}
		run(new IAction() {
			@Override
			public void perform() {
				listView.setAdapter(new RoomAdapter(ActivityRoom.this, items));
			}
		});
	}

	public void S_TABLE_PLAYER_JOIN(Message message) {
		String sessionID = message.readString();
		Log.d("S_TABLE_PLAYER_JOIN", "Player: " + sessionID);
	}

	public void S_PLAYER_BACK_ROOM(Message message) {
		String sessionID = message.readString();
		connection.sendMessage(message);
		Log.e("S_PLAYER_BACK_ROOM", "Player thoat game: " + sessionID);
	}

	public void S_PLAYER_JOIN_ROOM(Message message) {
		boolean check = message.readBoolean();
		String alert = message.readString();
		Log.e("S_PLAYER_JOIN_ROOM", (check + " - " + alert));
		if (check) {
			mPlayer.setIdRoom(idRoom);
			Intent intent = new Intent(this, ActivityTable.class);
			startActivity(intent);
		}
	}

	public void C_PLAYER_OUT_ROOM(int idRoom) {
		Message message = new Message(CMD.C_PLAYER_OUT_ROOM);
		message.writeInt(idRoom);
		connection.sendMessage(message);
	}

	public void C_PLAYER_GET_LIST_ROOM() {
		Message message = new Message(CMD.C_PLAYER_GET_LIST_ROOM);
		connection.sendMessage(message);
	}

	public void C_PLAYER_JOIN_ROOM(int idRoom) {
		Message message = new Message(CMD.C_PLAYER_JOIN_ROOM);
		message.writeInt(idRoom);
		connection.sendMessage(message);
	}

	@Override
	public void onBackPressed() {

		if (connection != null) {
			connection.closeSocket();
		}
		connection = null;
		doConnect();

		startActivity(ActivityLogin.class);
		finish();
	}

	@Override
	public void loadResources() {

	}

	@Override
	public void loadScenes() {

	}

	@Override
	public void processMessage(Message message) {
		switch (message.getCMD()) {
		case CMD.S_PLAYER_GET_LIST_ROOM:
			S_PLAYER_GET_LIST_ROOM(message);
			break;
		case CMD.S_PLAYER_JOIN_ROOM:
			S_PLAYER_JOIN_ROOM(message);
			break;
		case CMD.S_PLAYER_JOIN_TABLE:
			S_PLAYER_JOIN_TABLE(message);
			break;
		case CMD.S_TABLE_PLAYER_JOIN:
			S_TABLE_PLAYER_JOIN(message);
			break;
		case CMD.S_PLAYER_BACK_ROOM:
			S_PLAYER_BACK_ROOM(message);
			break;
		case CMD.S_PLAYER_GET_LIST_TABLE_INFO:
			showToast("Getted list table");
			S_PLAYER_GET_LIST_TABLE_INFO(message);
			break;
		default:
			break;
		}
	}

	@Override
	public void disConnection() {
		this.finish();
	}

}
